
require "sketchup.rb"
require "EventRelay.dll"

# ----------------------------------------------------------------------------
class EventRelayInfo < EventRelay
# ----------------------------------------------------------------------------

    SUPPRESS = true
    ALLOW = false

    # ----------------------------------
    #    initialize
    # ----------------------------------
    def initialize
        super
        @toolsObserver = MyToolsObserver.new
        Sketchup.active_model.tools.add_observer(@toolsObserver)
        # send Alt-Window-Ruby to open ruby console
        SendKeyMacro("!WR");
    end
    # ----------------------------------
    #    OnKeyDown
    # ----------------------------------
    def OnKeyDown(key,repeat,flags)
        printf("KEY DOWN key[%d] repeat[%d] flags[%#x]\n", key, repeat, flags)
        vk_key, keyMacro = GetKeyInfo(key, flags)
        puts("KEY INFO vk_key[#{vk_key}] keyMacro[#{keyMacro}]")
        return ALLOW
    end
    # ----------------------------------
    #    OnKeyUp
    # ----------------------------------
    def OnKeyUp(key,repeat,flags)
        printf("KEY UP key[%d] repeat[%d] flags[%#x]\n", key, repeat, flags)
        return ALLOW
    end
    # ----------------------------------
    #    Mouse OnLButtonDown
    # ----------------------------------
    def OnLButtonDown(flags, x, y)
		puts("LEFT BUTTON DOWN flags[#{flags}] x[#{x}] y[#{y}]")
        return ALLOW
    end
    # ----------------------------------
    #    Mouse OnLButtonUp
    # ----------------------------------
    def OnLButtonUp(flags, x, y)
		puts("LEFT BUTTON UP flags[#{flags}] x[#{x}] y[#{y}]")
        return ALLOW
    end
    # ----------------------------------
    #    Mouse OnRButtonUp
    # ----------------------------------
    def OnRButtonUp(flags, x, y)
        puts "RIGHT BUTTON UP flags[#{flags}] x[#{x}] y[#{y}]"
        return ALLOW
    end #OnRButtonUp
    # ----------------------------------
    #    Mouse OnRButtonDown
    # ----------------------------------
    def OnRButtonDown(flags, x, y)
        puts "RIGHT BUTTON DOWN flags[#{flags}] x[#{x}] y[#{y}]"
        return ALLOW
    end #OnRButtonDown
    # ----------------------------------
    #    Mouse OnMButtonUp
    # ----------------------------------
    def OnMButtonUp(flags, x, y)
        puts "MIDDLE BUTTON UP flags[#{flags}] x[#{x}] y[#{y}]"
        return ALLOW
    end #OnRButtonUp
    # ----------------------------------
    #    Mouse OnMButtonDown
    # ----------------------------------
    def OnMButtonDown(flags, x, y)
        puts "MIDDLE BUTTON DOWN flags[#{flags}] x[#{x}] y[#{y}]"
        return ALLOW
    end #OnRButtonDown
    # ----------------------------------
    #    Menus
    # ----------------------------------
    def OnMenuSelected(menuId, menuLabel)
        puts "MENU SELECTED MenuId[#{menuId}] label[#{menuLabel}]"
        # Make sure the tools observer is re-instated across file new/open/save as
        @toolsObserver.checkToolsObserver()
        return ALLOW
    end #OnMenuSelected

end # class EventRelayInfo
# ----------------------------------------------------------------------------
class MyToolsObserver < Sketchup::ToolsObserver
# ----------------------------------------------------------------------------
    # We need a Tools Observer to tell us when the MoveTool
    # is active. We need its state to tell us when an entity is
    # stuck to its cursor.

    def initialize
        super
        @currentModelId = Sketchup.active_model.object_id
        @currentTool = "notool"
        @currentToolState = 0
    end

    def onActiveToolChanged(tools_object, toolname, toolid)
        @currentTool = toolname
        puts "Active Tool Changed: #{@currentTool}"
    end
    def onToolStateChanged(tools_object, toolname, toolid, state)
        @currentTool = toolname
        @currentToolState = state
        #-puts "Tool State Changed: #{@currentTool} #{state}"
    end

    def checkToolsObserver
        if ( @currentModelId != Sketchup.active_model.object_id)
            #-printf("old[%d] new[%d]\n", @currentModelId, Sketchup.active_model.object_id)
            Sketchup.active_model.tools.remove_observer(self) #// probably not necessary
            @currentModelId = Sketchup.active_model.object_id
            Sketchup.active_model.tools.add_observer(self)
        end
    end

    def getActiveTool
        return @currentTool
    end

    def getToolState
        return @currentToolState
    end

end # class MyToolsObserver
# ----------------------------------------------------------------------------
unless file_loaded?("EventRelayInfo.rb")
    anEventRelayInfo = EventRelayInfo.new
    file_loaded "EventRelayInfo.rb"
end

